package winOrDie.graphicsInterface.GameRoom;

import java.util.ArrayList;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;

import winOrDie.Game.WoD_Game;
import winOrDie.Net.Client.WoD_Peer;
import winOrDie.graphicsInterface.WoD_Composite_Type;

public class Composite_Peer_List_NEW extends Composite {

	int compType;
	private Table table = null;

	public Composite_Peer_List_NEW(Composite parent, int style, int compType) {
		super(parent, style);
		this.compType = compType;
		initialize();
//		test();
	}

	private void initialize() {
		
		setSize(new Point(300, 200));
		setLayout(new GridLayout());
		get_Table();
	}

	
	private Table get_Table()
	{
		if (table == null)
		{
			GridData gridData = new GridData();
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.verticalAlignment = SWT.FILL;
			gridData.horizontalAlignment = SWT.FILL;
			
			
			table = new Table(this, SWT.BORDER);
			table.setHeaderVisible(false);
			table.setLinesVisible(false);
			table.setLayoutData(gridData);
		}
		
		return table;
	}
	
	
	public void test()
	{
//		TableItem item = new TableItem (table, SWT.NONE);
//		item.setText ("Table Item");
		//item.setImage (image);
		
//		ArrayList<WoD_Peer> woDPeers = new ArrayList<WoD_Peer>();
//		WoD_Peer woDPeer = new WoD_Peer();
//		woDPeer.setUserName("aaa1");
//		woDPeers.add(woDPeer);
//		WoD_Peer woDPeer2 = new WoD_Peer();
//		woDPeer2.setUserName("aaa2");
//		woDPeers.add(woDPeer2);
//		
//		paint_Table(woDPeers);
		
	}
	
	
	public void paint_Table(final ArrayList<WoD_Peer> woDPeers)
	{
		if (table.isDisposed()) {return;}
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				
				getDisplay().syncExec(new Runnable() {
					
					@Override
					public void run() {
						if (table.isDisposed()) {return;}
						table.removeAll();
					}
				});
				
								
				for (final WoD_Peer woDPeer : woDPeers) {
					if (isDisposed()) return;
					getDisplay().syncExec(new Runnable() {
						
						@Override
						public void run() {

							Image imgTick = new Image(getDisplay(), "img/tick.png");
							Image imgPirate = new Image(getDisplay(), "img/pirata_movD.png");
							Image imgCaptain = new Image(getDisplay(), "img/heroe_movD.png");
							Image imgPrincess = new Image(getDisplay(), "img/peach_movD.png");							
							
							if (table.isDisposed()) return;	
							TableItem item = new TableItem(table, SWT.NONE);
							item.setText(woDPeer.getUserName());
							
							if (compType == WoD_Composite_Type.PEER_COMPOSITE)
							{
								if(woDPeer.isReadyToGo())
									item.setImage(imgTick);
							}	
							if (compType == WoD_Composite_Type.TEAM_COMPOSITE)
							{
								if(woDPeer.getTeam() == WoD_Game.Game_Teams.Captain_Team){
									if(woDPeer.getROL() == WoD_Game.Game_Rols.Captain)
									{
										item.setImage(imgCaptain);
									}else if(woDPeer.getROL() == WoD_Game.Game_Rols.Princess)
									{
										item.setImage(imgPrincess);
									}										
								}									
								else if(woDPeer.getTeam() == WoD_Game.Game_Teams.Pirates_Team)
									item.setImage(imgPirate);
							}										

						}
					});
					
				}
				
			}
		};
		
		new Thread(runnable).start(); 
	}
	
}
